package com.yyge.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyge.entity.Employee;
import com.yyge.mapper.EmployeeMapper;
import com.yyge.service.EmployeeService;
import com.yyge.vo.PageVo;
import com.yyge.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource(type = EmployeeMapper.class)
    private EmployeeMapper mapper;

    @Override
    public R selectAll(Employee target) {

        // 设置分页
        PageHelper.startPage(target.getPageNum(), target.getPageNum());

        // 分页查询出的数据
        List<Employee> data = mapper.selectAll(target);

        PageInfo<Employee> pageInfo = PageInfo.of(data);

        // 从pageInfo中提取pageNum,pageSize,list,total，封装为pageVo
        PageVo pageVo = PageVo.of(pageInfo);
        return R.ok(pageVo);
    }

    @Override
    public R login(Employee verifyObj) {
        List<Employee> list= new ArrayList<>();
        Employee user=mapper.login(verifyObj);
        if(user!=null){
            list.add(user);
        }
        PageInfo<Employee> pageInfo = PageInfo.of(list);
        return user!=null?R.ok(pageInfo):R.fail("登陆失败！");
    }

    @Override
    public R insert(Employee employee) {
        int rows=mapper.insert(employee);
        return rows>0?R.ok("新增成功！"):R.fail("新增失败！");
    }

    @Override
    public R deleteByPrimaryKey(Integer key) {
        int rows=mapper.deleteByPrimaryKey(Long.valueOf(key));
        return rows>0?R.ok("删除成功！") :R.fail("删除失败！");
    }

    @Override
    public R updateByPrimaryKeySelective(Employee record) {
        int rows=mapper.updateByPrimaryKeySelective(record);
        return rows>0?R.ok("修改成功！") :R.fail("修改失败！");
    }

    @Override
    public R updateByPrimaryKey(Employee employee) {
        int rows=mapper.updateByPrimaryKey(employee);
        return rows>0?R.ok("修改成功！") :R.fail("修改失败！");
    }

    @Override
    public R selectByPrimaryKey(Integer key) {
        Employee employee =mapper.selectByPrimaryKey((long)key);
        return R.ok(employee);
    }
}
